package com.softeem.music_sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Music;
import com.softeem.music_sys.service.MusicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "音乐接口", description = "音乐相关操作：增删改查等")
@RestController
@RequestMapping("/music")
public class MusicController {

    @Autowired
    private MusicService musicService;

    // 添加音乐
    @Operation(summary = "添加音乐", description = "添加音乐，提供JSON格式音乐数据")
    @PostMapping
    public Result add(@RequestBody Music music) {
        boolean flag = musicService.save(music);
        return flag ? Result.success().setMsg("添加成功").setData(music) : Result.fail("添加失败");
    }

    // 修改音乐信息
    @Operation(summary = "修改音乐信息", description = "修改音乐信息，提供JSON格式音乐数据")
    @PutMapping
    public Result update(@RequestBody Music music) {
        boolean flag = musicService.updateById(music);
        return flag ? Result.success().setMsg("修改成功") : Result.fail("修改失败");
    }

    // 删除音乐
    @Operation(summary = "删除音乐", description = "根据音乐ID删除音乐")
    @Parameters({
            @Parameter(name = "id", description = "音乐ID")
    })
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        // 检查音乐是否存在
        Music music = musicService.getById(id);
        if (music == null) {
            return Result.fail("音乐不存在");
        }
        boolean flag = musicService.removeById(id);
        return flag ? Result.success().setMsg("删除成功") : Result.fail("删除失败");
    }

    // 音乐列表查询（分页）
    @Operation(summary = "音乐列表查询", description = "音乐列表查询，支持分页")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "每页数量")
    })
    @GetMapping("/list")
    public Result list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit
    ) {
        Page<Music> pageUtil = new Page<>(page, limit);
        List<Music> list = musicService.list(pageUtil);
        return Result.success().setData(list);
    }

    // 根据ID获取音乐信息
    @Operation(summary = "获取音乐信息", description = "根据音乐ID获取音乐详细信息")
    @Parameters({
            @Parameter(name = "id", description = "音乐ID")
    })
    @GetMapping("/{id}")
    public Result getMusicById(@PathVariable Integer id) {
        Music music = musicService.getById(id);
        if (music == null) {
            return Result.fail("音乐不存在");
        }
        return Result.success().setData(music);
    }

    // 根据专辑ID查询音乐列表
    @Operation(summary = "根据专辑ID查询音乐", description = "根据专辑ID查询该专辑下的所有音乐")
    @Parameters({
            @Parameter(name = "albumId", description = "专辑ID")
    })
    @GetMapping("/byAlbum/{albumId}")
    public Result getMusicByAlbumId(@PathVariable Integer albumId) {
        List<Music> list = musicService.list(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Music>().eq("album_id", albumId));
        return Result.success().setData(list);
    }
}